package com.hsd.znsh.service.impl;

import com.hsd.znsh.converter.GatewayConverter;
import com.hsd.znsh.entity.Event;
import com.hsd.znsh.entity.Gateway;
import com.hsd.znsh.entity.Host;
import com.hsd.znsh.entity.Sensor;
import com.hsd.znsh.enums.OnlineStatusEnum;
import com.hsd.znsh.enums.ReadStatusEnum;
import com.hsd.znsh.enums.ResultEnum;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.pojo.output.GatewayVO;
import com.hsd.znsh.repository.EventRepository;
import com.hsd.znsh.repository.GatewayRepository;
import com.hsd.znsh.repository.HostRepository;
import com.hsd.znsh.service.GatewayService;
import com.hsd.znsh.service.LoraService;
import com.hsd.znsh.service.PushService;
import com.hsd.znsh.utils.BeanUtil;
import com.hsd.znsh.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-07-26 18:25:14
 */
@Service
public class GatewayServiceImpl extends BaseServiceImpl implements GatewayService{

    @Autowired
    private GatewayRepository gatewayRepository;

    @Autowired
    private HostRepository hostRepository;

    @Autowired
    private EventRepository eventRepository;

    @Autowired
    private PushService pushService;

    @Autowired
    private LoraService loraService;

    @Override
    public Gateway addGateway(Gateway gateway) {
        if(gatewayRepository.findByGatewayId(gateway.getGatewayId())!=null){
            throw new ZnshException(ResultEnum.GATEWAY_ID_EXITS);
        }
        if(!loraService.addGateway(gateway)) throw new ZnshException("添加失败，请重新惭怍");
        gateway.setMessage("网关未上电，请上电");
        gateway.setOnlineStatus(OnlineStatusEnum.NOT_ONLINE.getCode());
        gateway.setTime(new Date());
        return gatewayRepository.save(gateway);
    }

    @Override
    public Gateway updateGateway(Gateway gateway) {
        Gateway target=gatewayRepository.findOne(gateway.getId());
        if(target==null) return null;
        BeanUtil.copyPropertiesIgnoreNull(gateway,target);
        return gatewayRepository.save(target);
    }

    @Override
    public void deleteGateway(String gatewayId) {
        if(!loraService.deleteGateway(gatewayId)) throw new ZnshException("删除失败，请重新操作");
        gatewayRepository.deleteByGatewayId(gatewayId);
    }

    @Override
    public GatewayVO getGatewayInfoByGatewayId(String gatewayId) {
        Gateway gateway=gatewayRepository.findByGatewayId(gatewayId);
        return GatewayConverter.converterToGatewayVO(gateway,hostRepository);
    }

    @Override
    public List<Gateway> getGatewayListByHostSn(String hostSn) {
        Host host=hostRepository.findByHostSn(hostSn);
        return gatewayRepository.findByHostId(host.getId());
    }

    @Override
    public Gateway updateGatewayStatus(Gateway gateway) {
        Gateway target=gatewayRepository.findByGatewayId(gateway.getGatewayId());
        if(target==null) return null;
        BeanUtil.copyPropertiesIgnoreNull(gateway,target);
        Date date=new Date();
        if(gateway.getOnlineStatus()== OnlineStatusEnum.ONLINE.getCode()){
            push(target,date);
            target.setTime(date);
        }
        return gatewayRepository.save(target);
    }

    private void push(Gateway gateway, Date date){
        LocalDateTime gatewayTime= DateUtil.dateToLocalDateTime(gateway.getTime());
        LocalDateTime nowTime=DateUtil.dateToLocalDateTime(date);
        if(gateway.getOnlineStatus() == OnlineStatusEnum.NOT_ONLINE.getCode()&&
                gatewayTime.plusMinutes(2).isBefore(nowTime)){
            System.out.println("网关上线"+DateUtil.format(date,"yyyy-MM-dd HH:mm:ss"));
            Host host=hostRepository.findOne(gateway.getHostId());
            String content;
            String title;
            if (gateway.getAlias() == null || gateway.getAlias().equals("")) {
                content = gateway.getName() + "(" + gateway.getGatewayId() + ") 上线";
            }else{
                content = gateway.getAlias() + "(" + gateway.getGatewayId() + ") 上线";
            }
            if (host.getAlias() == null || host.getAlias().equals("")) {
                title = host.getHostName();
            }else{
                title = host.getAlias();
            }
            Event event=new Event();
            event.setReadStatus(ReadStatusEnum.NOT_READ.getCode());
            event.setContent(content);
            event.setDeviceSn(gateway.getGatewayId() );
            event.setEventLevel(1);
            event.setGatewayName(title);
            event.setEventType("通知");
            event.setGatewaySn(host.getHostSn());

            eventRepository.save(event);
            pushService.pushToGroup(title,content,host.getHostSn());
        }
    }
}
